/**
 *
 */
import Util;
import OSS;
import OpenPlatform;
import OSSUtil;
import FileForm;
import OpenApi;
import OpenApiUtil;
import EndpointUtil;

extends OpenApi;


init(config: OpenApi.Config){
  super(config);
  @endpointRule = '';
  
  checkConfig(config);
  @endpoint = getEndpoint('imagesearch', @regionId, @endpointRule, @network, @suffix, @endpointMap, @endpoint);
}

function getEndpoint(productId: string, regionId: string, endpointRule: string, network: string, suffix: string, endpointMap: map[string]string, endpoint: string) throws: string{
  if (!Util.empty(endpoint)) {
    return endpoint;
  }
  
  if (!Util.isUnset(endpointMap) && !Util.empty(endpointMap[regionId])) {
    return endpointMap[regionId];
  }
  return EndpointUtil.getEndpointRules(productId, regionId, endpointRule, network, suffix);
}

model GetProductInfoByIdsRequest {
  fields?: string(name='Fields', example='Url,UserType'),
  itemIds?: string(name='ItemIds', example='111,22,33'),
  pid?: string(name='Pid', example='mm_xxx_xxx_xxx'),
}

model GetProductInfoByIdsResponseBody = {
  code?: int32(name='Code', example='0'),
  data?: {
    auctions?: [ 
      {
        rankScore?: float(name='RankScore', example='0.34'),
        result?: {
          categoryName?: string(name='CategoryName'),
          commissionRate?: string(name='CommissionRate', example='180'),
          couponAmount?: int32(name='CouponAmount', example='8'),
          couponEndTime?: string(name='CouponEndTime', example='2021-06-01'),
          couponInfo?: string(name='CouponInfo'),
          couponRemainCount?: int32(name='CouponRemainCount', example='0'),
          couponShareUrl?: string(name='CouponShareUrl', example='tbopen://m.taobao.com/tbopen/index.html?'),
          couponStartFee?: string(name='CouponStartFee', example='98.000000'),
          couponStartTime?: string(name='CouponStartTime', example='2021-05-01'),
          couponTotalCount?: string(name='CouponTotalCount', example='10000'),
          deeplinkCouponShareUrl?: string(name='DeeplinkCouponShareUrl', example='tbopen://m.taobao.com/tbopen/index.html?'),
          deeplinkUrl?: string(name='DeeplinkUrl', example='tbopen://m.taobao.com/tbopen/index.html?'),
          inputItemId?: string(name='InputItemId'),
          itemId?: string(name='ItemId', example='123'),
          levelOneCategoryName?: string(name='LevelOneCategoryName'),
          maxCommission?: {
            maxCommissionClickUrl?: string(name='MaxCommissionClickUrl', example='https://s.click.taobao.com/t?e=xxxx'),
            maxCommissionCouponShareUrl?: string(name='MaxCommissionCouponShareUrl', example='https://s.click.taobao.com/t?e=xxxx'),
            maxCommissionRate?: string(name='MaxCommissionRate', example='20.4'),
          }(name='MaxCommission'),
          nick?: string(name='Nick'),
          picUrl?: string(name='PicUrl', example='//img.alicdn.com/i1/FXXXXXXXXXX.jpg'),
          priceAfterCoupon?: string(name='PriceAfterCoupon', example='90'),
          provcity?: string(name='Provcity'),
          reservePrice?: string(name='ReservePrice', example='150'),
          sellerId?: string(name='SellerId', example='32590610'),
          shopTitle?: string(name='ShopTitle'),
          shortTitle?: string(name='ShortTitle'),
          subTitle?: string(name='SubTitle'),
          title?: string(name='Title'),
          url?: string(name='Url', example='tbopen://m.taobao.com/tbopen/index.html?'),
          userType?: int32(name='UserType', example='0'),
          volume?: int32(name='Volume', example='500'),
          zkFinalPrice?: string(name='ZkFinalPrice', example='98'),
        }(name='Result'),
      }
    ](name='Auctions'),
  }(name='Data'),
  message?: string(name='Message', example='success'),
  requestId?: string(name='RequestId', example='B3137727-7D6E-488C-BA21-0E034C38A879'),
  success?: boolean(name='Success', example='true'),
}

model GetProductInfoByIdsResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: GetProductInfoByIdsResponseBody(name='body'),
}

async function getProductInfoByIdsWithOptions(request: GetProductInfoByIdsRequest, runtime: Util.RuntimeOptions): GetProductInfoByIdsResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.fields)) {
    body['Fields'] = request.fields;
  }
  if (!Util.isUnset(request.itemIds)) {
    body['ItemIds'] = request.itemIds;
  }
  if (!Util.isUnset(request.pid)) {
    body['Pid'] = request.pid;
  }
  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'GetProductInfoByIds',
    version = '2021-05-01',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function getProductInfoByIds(request: GetProductInfoByIdsRequest): GetProductInfoByIdsResponse {
  var runtime = new Util.RuntimeOptions{};
  return getProductInfoByIdsWithOptions(request, runtime);
}

model SearchByPicRequest {
  categoryId?: int32(name='CategoryId', example='88888888'),
  crop?: boolean(name='Crop', example='true'),
  fields?: string(name='Fields', example='Url,UserType'),
  num?: int32(name='Num', example='10'),
  picContent?: string(name='PicContent', example='xxx'),
  pid?: string(name='Pid', example='mm_xxx_xxx_xxx'),
  region?: string(name='Region', example='280,486,232,351'),
  relationId?: long(name='RelationId', example='1125526'),
  start?: int32(name='Start', example='0'),
  userType?: long(name='UserType', example='0'),
}

model SearchByPicAdvanceRequest {
  categoryId?: int32(name='CategoryId', example='88888888'),
  crop?: boolean(name='Crop', example='true'),
  fields?: string(name='Fields', example='Url,UserType'),
  num?: int32(name='Num', example='10'),
  picContentObject?: readable(name='PicContent', example='xxx'),
  pid?: string(name='Pid', example='mm_xxx_xxx_xxx'),
  region?: string(name='Region', example='280,486,232,351'),
  relationId?: long(name='RelationId', example='1125526'),
  start?: int32(name='Start', example='0'),
  userType?: long(name='UserType', example='0'),
}

model SearchByPicResponseBody = {
  code?: int32(name='Code', example='0'),
  data?: {
    auctions?: [ 
      {
        rankScore?: float(name='RankScore', example='0.337'),
        result?: {
          categoryName?: string(name='CategoryName'),
          commissionRate?: string(name='CommissionRate', example='180'),
          couponAmount?: int32(name='CouponAmount', example='5'),
          couponEndTime?: string(name='CouponEndTime', example='2021-06-01'),
          couponInfo?: string(name='CouponInfo'),
          couponRemainCount?: int32(name='CouponRemainCount', example='0'),
          couponShareUrl?: string(name='CouponShareUrl', example='//uland.taobao.com/xxxxxxx'),
          couponStartFee?: string(name='CouponStartFee', example='98.000000'),
          couponStartTime?: string(name='CouponStartTime', example='2021-05-01'),
          couponTotalCount?: string(name='CouponTotalCount', example='100000'),
          deeplinkCouponShareUrl?: string(name='DeeplinkCouponShareUrl', example='tbopen://m.taobao.com/tbopen/index.html?'),
          deeplinkUrl?: string(name='DeeplinkUrl', example='tbopen://m.taobao.com/tbopen/index.html?'),
          itemId?: string(name='ItemId', example='123'),
          levelOneCategoryName?: string(name='LevelOneCategoryName'),
          maxCommission?: {
            maxCommissionClickUrl?: string(name='MaxCommissionClickUrl', example='https://s.click.taobao.com/t?e=xxxx'),
            maxCommissionCouponShareUrl?: string(name='MaxCommissionCouponShareUrl', example='https://uland.taobao.com/coupon/xxxx'),
            maxCommissionRate?: string(name='MaxCommissionRate', example='20.4'),
          }(name='MaxCommission'),
          nick?: string(name='Nick'),
          picUrl?: string(name='PicUrl', example='//img.alicdn.com/i1/FXXXXXXXXXX.jpg'),
          priceAfterCoupon?: string(name='PriceAfterCoupon', example='90'),
          provcity?: string(name='Provcity'),
          reservePrice?: string(name='ReservePrice', example='158'),
          sellerId?: string(name='SellerId', example='32590610'),
          shopTitle?: string(name='ShopTitle'),
          shortTitle?: string(name='ShortTitle'),
          subTitle?: string(name='SubTitle'),
          title?: string(name='Title'),
          url?: string(name='Url', example='//s.click.taobao.com/t?xxxxxx'),
          userType?: int32(name='UserType', example='0'),
          volume?: int32(name='Volume', example='500'),
          zkFinalPrice?: string(name='ZkFinalPrice', example='98'),
        }(name='Result'),
      }
    ](name='Auctions'),
  }(name='Data'),
  message?: string(name='Message', example='sucess'),
  picInfo?: {
    mainRegion?: {
      multiCategoryId?: [ 
        {
          categoryId?: int32(name='CategoryId', example='88888888'),
          score?: float(name='Score', example='0.34'),
        }
      ](name='MultiCategoryId'),
      region?: string(name='Region', example='280,486,232,351'),
    }(name='MainRegion'),
    multiRegion?: [ 
      {
        region?: string(name='Region', example='280,486,232,351'),
      }
    ](name='MultiRegion'),
  }(name='PicInfo'),
  requestId?: string(name='RequestId', example='B3137727-7D6E-488C-BA21-0E034C38A879'),
  success?: boolean(name='Success', example='true'),
}

model SearchByPicResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: SearchByPicResponseBody(name='body'),
}

async function searchByPicWithOptions(request: SearchByPicRequest, runtime: Util.RuntimeOptions): SearchByPicResponse {
  Util.validateModel(request);
  var query = {};
  if (!Util.isUnset(request.userType)) {
    query['UserType'] = request.userType;
  }
  var body : map[string]any = {};
  if (!Util.isUnset(request.categoryId)) {
    body['CategoryId'] = request.categoryId;
  }
  if (!Util.isUnset(request.crop)) {
    body['Crop'] = request.crop;
  }
  if (!Util.isUnset(request.fields)) {
    body['Fields'] = request.fields;
  }
  if (!Util.isUnset(request.num)) {
    body['Num'] = request.num;
  }
  if (!Util.isUnset(request.picContent)) {
    body['PicContent'] = request.picContent;
  }
  if (!Util.isUnset(request.pid)) {
    body['Pid'] = request.pid;
  }
  if (!Util.isUnset(request.region)) {
    body['Region'] = request.region;
  }
  if (!Util.isUnset(request.relationId)) {
    body['RelationId'] = request.relationId;
  }
  if (!Util.isUnset(request.start)) {
    body['Start'] = request.start;
  }
  var req = new OpenApi.OpenApiRequest{ 
    query = OpenApiUtil.query(query),
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'SearchByPic',
    version = '2021-05-01',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function searchByPic(request: SearchByPicRequest): SearchByPicResponse {
  var runtime = new Util.RuntimeOptions{};
  return searchByPicWithOptions(request, runtime);
}

async function searchByPicAdvance(request: SearchByPicAdvanceRequest, runtime: Util.RuntimeOptions): SearchByPicResponse {
  // Step 0: init client
  var accessKeyId = @credential.getAccessKeyId();
  var accessKeySecret = @credential.getAccessKeySecret();
  var securityToken = @credential.getSecurityToken();
  var credentialType = @credential.getType();
  var openPlatformEndpoint = @openPlatformEndpoint;
  if(Util.isUnset(openPlatformEndpoint)) {
    openPlatformEndpoint ='openplatform.aliyuncs.com';
  }
  if(Util.isUnset(credentialType)) {
    credentialType ='access_key';
  }
  var authConfig = new OpenApi.Config{
    accessKeyId = accessKeyId,
    accessKeySecret = accessKeySecret,
    securityToken = securityToken,
    type = credentialType,
    endpoint = openPlatformEndpoint,
    protocol = @protocol,
    regionId = @regionId,
  };
  var authClient = new OpenPlatform(authConfig);
  var authRequest = new OpenPlatform.AuthorizeFileUploadRequest{
    product = 'ImageSearch',
    regionId = @regionId,
  };
  var authResponse = new OpenPlatform.AuthorizeFileUploadResponse{};
  var ossConfig = new OSS.Config{
    accessKeySecret = accessKeySecret,
    type = 'access_key',
    protocol = @protocol,
    regionId = @regionId,
  };
  var ossClient : OSS = null;

  var fileObj = new FileForm.FileField{};
  var ossHeader = new OSS.PostObjectRequest.header{};
  var uploadRequest = new OSS.PostObjectRequest{};
  var ossRuntime = new OSSUtil.RuntimeOptions{};
  OpenApiUtil.convert(runtime, ossRuntime);
  var searchByPicReq = new SearchByPicRequest{};
  OpenApiUtil.convert(request, searchByPicReq);

  if(!Util.isUnset(request.picContentObject)) {
    authResponse = authClient.authorizeFileUploadWithOptions(authRequest, runtime);
    ossConfig.accessKeyId = authResponse.body.accessKeyId;
    ossConfig.endpoint = OpenApiUtil.getEndpoint(authResponse.body.endpoint, authResponse.body.useAccelerate, @endpointType);
    ossClient = new OSS(ossConfig);

    fileObj = new FileForm.FileField{
      filename = authResponse.body.objectKey,
      content = request.picContentObject,
      contentType = '',
    };
    ossHeader = new OSS.PostObjectRequest.header{
      accessKeyId = authResponse.body.accessKeyId,
      policy = authResponse.body.encodedPolicy,
      signature = authResponse.body.signature,
      key = authResponse.body.objectKey,
      file = fileObj,
      successActionStatus = '201',
    };
    uploadRequest = new OSS.PostObjectRequest{
      bucketName = authResponse.body.bucket,
      header = ossHeader,
    };
    ossClient.postObject(uploadRequest, ossRuntime);
    searchByPicReq.picContent = `http://${authResponse.body.bucket}.${authResponse.body.endpoint}/${authResponse.body.objectKey}`;
  }
  var searchByPicResp = searchByPicWithOptions(searchByPicReq, runtime);
  return searchByPicResp;
}

model SearchByUrlRequest {
  categoryId?: int32(name='CategoryId', example='88888888'),
  crop?: boolean(name='Crop', example='true'),
  fields?: string(name='Fields', example='Url,UserType'),
  num?: int32(name='Num', example='10'),
  picUrl?: string(name='PicUrl', example='https://oss-test.oss-cn-shanghai.aliyuncs.com/xxxx.jpg'),
  pid?: string(name='Pid', example='mm_xxx_xxx_xxx'),
  region?: string(name='Region', example='280,486,232,351'),
  relationId?: long(name='RelationId', example='1125526'),
  start?: int32(name='Start', example='0'),
  userType?: long(name='UserType', example='0'),
}

model SearchByUrlResponseBody = {
  code?: int32(name='Code', example='0'),
  data?: {
    auctions?: [ 
      {
        rankScore?: float(name='RankScore', example='0.337'),
        result?: {
          categoryName?: string(name='CategoryName'),
          commissionRate?: string(name='CommissionRate', example='180'),
          couponAmount?: int32(name='CouponAmount', example='5'),
          couponEndTime?: string(name='CouponEndTime', example='2021-06-01'),
          couponInfo?: string(name='CouponInfo'),
          couponRemainCount?: int32(name='CouponRemainCount', example='0'),
          couponShareUrl?: string(name='CouponShareUrl', example='//uland.taobao.com/cxxxxxx'),
          couponStartFee?: string(name='CouponStartFee', example='98.000000'),
          couponStartTime?: string(name='CouponStartTime', example='2021-05-01'),
          couponTotalCount?: string(name='CouponTotalCount', example='100000'),
          deeplinkCouponShareUrl?: string(name='DeeplinkCouponShareUrl', example='tbopen://m.taobao.com/tbopen/index.html?'),
          deeplinkUrl?: string(name='DeeplinkUrl', example='tbopen://m.taobao.com/tbopen/index.html?'),
          itemId?: string(name='ItemId', example='123'),
          levelOneCategoryName?: string(name='LevelOneCategoryName'),
          maxCommission?: {
            maxCommissionClickUrl?: string(name='MaxCommissionClickUrl', example='https://s.click.taobao.com/t?e=xxxx'),
            maxCommissionCouponShareUrl?: string(name='MaxCommissionCouponShareUrl', example='https://s.click.taobao.com/t?e=xxxx'),
            maxCommissionRate?: string(name='MaxCommissionRate', example='20.4'),
          }(name='MaxCommission'),
          nick?: string(name='Nick'),
          picUrl?: string(name='PicUrl', example='//img.alicdn.com/i1/FXXXXXXXXXX.jpg'),
          priceAfterCoupon?: string(name='PriceAfterCoupon', example='90'),
          provcity?: string(name='Provcity'),
          reservePrice?: string(name='ReservePrice', example='158'),
          sellerId?: string(name='SellerId', example='32590610'),
          shopTitle?: string(name='ShopTitle'),
          shortTitle?: string(name='ShortTitle'),
          subTitle?: string(name='SubTitle'),
          title?: string(name='Title'),
          url?: string(name='Url', example='//s.click.taobao.com/t?xxxxxx'),
          userType?: int32(name='UserType', example='0'),
          volume?: int32(name='Volume', example='500'),
          zkFinalPrice?: string(name='ZkFinalPrice', example='98'),
        }(name='Result'),
      }
    ](name='Auctions'),
  }(name='Data'),
  message?: string(name='Message', example='success'),
  picInfo?: {
    mainRegion?: {
      multiCategoryId?: [ 
        {
          categoryId?: int32(name='CategoryId', example='88888888'),
          score?: float(name='Score', example='0.34'),
        }
      ](name='MultiCategoryId'),
      region?: string(name='Region', example='280,486,232,351'),
    }(name='MainRegion'),
    multiRegion?: [ 
      {
        region?: string(name='Region', example='280,486,232,351'),
      }
    ](name='MultiRegion'),
  }(name='PicInfo'),
  requestId?: string(name='RequestId', example='B3137727-7D6E-488C-BA21-0E034C38A879'),
  success?: boolean(name='Success', example='true'),
}

model SearchByUrlResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: SearchByUrlResponseBody(name='body'),
}

async function searchByUrlWithOptions(request: SearchByUrlRequest, runtime: Util.RuntimeOptions): SearchByUrlResponse {
  Util.validateModel(request);
  var query = {};
  if (!Util.isUnset(request.userType)) {
    query['UserType'] = request.userType;
  }
  var body : map[string]any = {};
  if (!Util.isUnset(request.categoryId)) {
    body['CategoryId'] = request.categoryId;
  }
  if (!Util.isUnset(request.crop)) {
    body['Crop'] = request.crop;
  }
  if (!Util.isUnset(request.fields)) {
    body['Fields'] = request.fields;
  }
  if (!Util.isUnset(request.num)) {
    body['Num'] = request.num;
  }
  if (!Util.isUnset(request.picUrl)) {
    body['PicUrl'] = request.picUrl;
  }
  if (!Util.isUnset(request.pid)) {
    body['Pid'] = request.pid;
  }
  if (!Util.isUnset(request.region)) {
    body['Region'] = request.region;
  }
  if (!Util.isUnset(request.relationId)) {
    body['RelationId'] = request.relationId;
  }
  if (!Util.isUnset(request.start)) {
    body['Start'] = request.start;
  }
  var req = new OpenApi.OpenApiRequest{ 
    query = OpenApiUtil.query(query),
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'SearchByUrl',
    version = '2021-05-01',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function searchByUrl(request: SearchByUrlRequest): SearchByUrlResponse {
  var runtime = new Util.RuntimeOptions{};
  return searchByUrlWithOptions(request, runtime);
}

